package com.isoftstone.app.controller.paymentSign;

import com.isoftstone.bank.controller.base.BaseController;
import com.isoftstone.common.Tidings;
import com.isoftstone.merchant.entity.feeUser.MFeeUser;
import com.isoftstone.merchant.entity.mdept.MDept;
import com.isoftstone.merchant.entity.merchant.Merchant;
import com.isoftstone.merchant.service.feeUser.impl.FeeUserServiceImpl;
import com.isoftstone.merchant.service.mdept.impl.MdeptService;
import com.isoftstone.merchant.service.merchant.impl.MerchantServiceImpl;
import com.isoftstone.util.Const;
import com.isoftstone.util.PropertiesUtils;
import com.isoftstone.util.bocop.BocopUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: zhaojg
 * @Description
 * @Date: Created in 17:08 2017/12/5
 * @Modified by:
 * 缴费签约项目接口
 */
@Controller
public class PaymentSignController extends BaseController {

    @Autowired
    MerchantServiceImpl merchantService;

    @Autowired
    FeeUserServiceImpl feeUserService;

    @Autowired
    MdeptService mdeptService;

    /**
     * 点击缴费签约模块时跳转，查询出已经签约的项目
     */
    @RequestMapping(value = "app/admin/QuerySignProject", method = RequestMethod.POST)
    @ResponseBody
    public Tidings<Map<String, Object>> toSignProject(String openAccount) {
        Tidings<Map<String, Object>> tidings = new Tidings<Map<String, Object>>();
        Map<String, Object> reMap = new HashMap<String, Object>();
        List<Merchant> paymentSignProject = null;
        try {
            paymentSignProject = merchantService.getPaymentSignProject(openAccount);
            for (Merchant merchant : paymentSignProject) {
                String logoFile = merchant.getLogoFile();
                if (logoFile != null) {
                    String path = PropertiesUtils.getProperties("baseContextPath") + Const.FILEPATHIMG + logoFile;
                    merchant.setLogoFile(path);
                }
            }
            reMap.put("paymentSignProject", paymentSignProject);
            tidings.setMsg("查询成功");
            tidings.setStatus(Const.SUCCESS);
            tidings.setT(reMap);
        } catch (Exception e) {
            tidings.setMsg("查询失败");
            tidings.setStatus(Const.ERROR);
            e.printStackTrace();
        }
        return tidings;
    }

    /**
     * 查询已签约项目详情：分两个接口
     * 1.查询签约该缴费单位下的所有缴费编号
     * 2.查询详情页面的其他信息
     */
    /**
     * 查询缴费编号
     *
     * @param corpId      缴费单位id 唯一
     * @param openAccount 易商登录用户账户
     * @return MFeeUser
     */
    @RequestMapping(value = "app/admin/QuerySignProjectDetail", method = RequestMethod.POST)
    @ResponseBody
    public Tidings<Map<String, Object>> queryFeeNO(String corpId, String openAccount) {
        Tidings<Map<String, Object>> tidings = new Tidings<Map<String, Object>>();
        Map<String, Object> reMap = new HashMap<String, Object>();
//        corpId = "41";
//        openAccount = "xpfish";
        List<MFeeUser> mFeeUsers = null;
        try {
            mFeeUsers = feeUserService.queryFeeIdByCropId(corpId, openAccount);
            for (MFeeUser mFeeUser : mFeeUsers) {
                String feeId = mFeeUser.getFeeId();
                String corpId1 = mFeeUser.getCorpId();
                //根据缴费编号和商户id查询的缴费详情
                MFeeUser feeUser = queryPaymentDetailByFeeId(feeId, corpId1);
                if (feeUser != null) {
                    //根据机构id查询机构树
                    String deptId = feeUser.getDeptId();
                    List<MDept> superList = mdeptService.getUpAlltree(deptId);
                    //将dept的list列表进行倒转
                    Collections.reverse(superList);
                    feeUser.setmDept(superList);
                    mFeeUser.setFeeUser(feeUser);
                }
            }
            reMap.put("mFeeUsers", mFeeUsers);
            tidings.setMsg("查询成功");
            tidings.setStatus(Const.SUCCESS);
            tidings.setT(reMap);
        } catch (Exception e) {
            tidings.setMsg("查询失败");
            tidings.setStatus(Const.ERROR);
            e.printStackTrace();
        }
        return tidings;
    }

    /**
     * 查询缴费详情
     *
     * @param feeId  缴费编号
     * @param corpId 缴费单位id
     * @return
     */
    public MFeeUser queryPaymentDetailByFeeId(String feeId, String corpId) {
        MFeeUser feeUser = null;
        try {
            feeUser = feeUserService.queryFeeUserByFeeId(feeId, corpId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return feeUser;
    }

    /**
     * 根据缴费编号查询缴费信息
     *
     * @param feeId 缴费编号
     * @return MFeeUser
     */
//    @RequestMapping(value = "app/admin/queryFeeUserByFeeId",method = RequestMethod.POST)
//    @ResponseBody
//    public Tidings<Map<String, Object>> queryPaymentDetailByFeeId(String feeId){
//        feeId = "5689798";
//        Tidings<Map<String, Object>> tidings = new Tidings<Map<String, Object>>();
//        Map<String, Object> reMap = new HashMap<String, Object>();
//        MFeeUser feeUser = null;
//        try {
//            feeUser = feeUserService.queryFeeUserByFeeId(feeId);
//            reMap.put("feeUser",feeUser);
//            tidings.setMsg("查询成功");
//            tidings.setStatus(Const.SUCCESS);
//            tidings.setT(reMap);
//        } catch (Exception e) {
//            tidings.setMsg("查询失败");
//            tidings.setStatus(Const.ERROR);
//            e.printStackTrace();
//        }
//        return tidings;
//    }

    /**
     * 发送手机验证码
     *
     * @param request
     * @return Object
     */
    @RequestMapping(value = "app/admin/getPhoneCode", method = RequestMethod.POST)
    @ResponseBody
    public Object getCode(HttpServletRequest request) {
        Tidings<Map<String, String>> tidings = new Tidings<Map<String, String>>();
        Map<String, String> reMap = new HashMap<>();
        try {
            reMap = BocopUtils.getCode(request);
            tidings.setT(reMap);
            tidings.setStatus(Const.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            tidings.setStatus(Const.ERROR);
        }
        return tidings;
    }

    /**
     * 验证码发送到boc验证正确性
     *
     * @param request
     * @return Object
     */
    @RequestMapping(value = "app/admin/checkPhoneCode", method = RequestMethod.POST)
    @ResponseBody
    public Object verifyCode(HttpServletRequest request) {
        Tidings<String> tidings = new Tidings<String>();
        try {
            String string = BocopUtils.verifyCode(request);
            tidings.setT(string);
            tidings.setStatus(Const.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            tidings.setStatus(Const.ERROR);
        }
        return tidings;
    }
}
